package com.wn.ziroom.controller;


import com.wn.utils.CookieUtils;
import com.wn.utils.JwtUtils;
import com.wn.utils.SnowFlowUtils;
import com.wn.ziroom.entity.CartItem;
import com.wn.ziroom.entity.Provinces;
import com.wn.ziroom.entity.User;
import com.wn.ziroom.service.CartService;
import com.wn.ziroom.service.OrderService;
import com.wn.ziroom.service.ProvincesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wangzhijia
 * @since 2023-02-23 02:27:54
 */
@Controller
@RequestMapping("/ziroom_portal/order")
public class OrderController {

    private ProvincesService provincesService;
    private CartService cartService;
    private OrderService orderService;

    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }

    @Autowired
    public void setCartService(CartService cartService) {
        this.cartService = cartService;
    }

    @Autowired
    public void setProvincesService(ProvincesService provincesService) {
        this.provincesService = provincesService;
    }

    @RequestMapping("/goToOrder")
    public ModelAndView goToOrder(HttpServletRequest request,
                                  @RequestParam("total") BigDecimal total,
                                  @RequestParam("productionIds") String[] productionIds){

        ModelAndView mav = new ModelAndView();

        //等到用户信息
        String token = CookieUtils.getCookie("token", request);
        User user = JwtUtils.verifyTokenWithoutVerifyExp(token, User.class);
        Integer userId = user.getId();

        List<CartItem> cartItemList = cartService.findCartByUserIdAndProIds(userId,productionIds);
        mav.addObject("cartItemList",cartItemList);

        //总价格
        mav.addObject("total",total);

        //省的列表
        List<Provinces> provincesList = provincesService.showAll();
        mav.addObject("provincesList",provincesList);

        mav.setViewName("order");

        //-- 获取订单明细


        return mav;
    }

    @ResponseBody
    @PostMapping("/createOrder")
    public String createOrder(@RequestParam("provinces") String provinces,
                              @RequestParam("city") String city,
                              @RequestParam("area") String area,
                              @RequestParam("street") String street,
                              @RequestParam("village") String village,
                              @RequestParam("address") String address,
                              @RequestParam("productionId") String productionId,
                              @RequestParam("total") BigDecimal total,
                              HttpServletRequest request){

        /*System.out.println("省:"+provinces+"市:"+city+"区:"+area+"街道:"+street+"村:"+village+"详细地址:"+address+"==总价:=="+total);

        String[] strings = productionId.split(",");
        for (String s : strings) {
            System.out.println("ids---->"+s);
        }*/

        //-- 生成订单号
        long orderNumber = SnowFlowUtils.getInstance().nextId();

        //-- 将订单写入数据库,此时状态为 1 (未支付)
        String token2 = CookieUtils.getCookie("token", request);
        User user = JwtUtils.verifyTokenWithoutVerifyExp(token2, User.class);
        Integer userId = user.getId();

        int i = orderService.createNewOrder(orderNumber,userId,provinces,city,area,street,village,address,productionId,total);

        //-- 跳转支付,创建队列   支付成功后再删除购物车

        return "orderNumber:"+orderNumber+",total:"+total+",productionId:"+productionId;
    }

}

